
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>流转网络管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input
          v-model="formInline.name"
          placeholder="链单网络名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-select v-model="formInline.bank_id" placeholder="融资机构">
          <el-option
            v-for="item in bankOption"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-select v-model="formInline.status" placeholder="状态">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search"
          >搜索</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="createNetworkDialog = true"
          >添加</el-button
        >
        <el-dialog title="添加链单网络" :visible.sync="createNetworkDialog">
          <el-form :model="addForm" label-position="left">
            <el-row>
              <el-col :span="12">
                <el-form-item label="链单网络名称">
                  <el-input
                    v-model="addForm.name"
                    autocomplete="off"
                    style="width: 220px"
                  ></el-input> </el-form-item
              ></el-col>
              <el-col :span="12">
                <el-form-item label="网络编号">
                  <el-input
                    placeholder="系统自动生成"
                    :disabled="true"
                    style="width: 220px"
                  ></el-input> </el-form-item
              ></el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="业务产品类型">
                  <el-select
                    v-model="addForm.type"
                    placeholder="请选择业务产品类型"
                  >
                    <el-option
                      label="授信保理链单"
                      value="授信保理链单"
                    ></el-option>
                    <el-option
                      label="非授信链单"
                      value="非授信链单"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="融资机构">
                  <el-select
                    v-model="addForm.bank_id"
                    placeholder="请选择融资机构"
                  >
                    <el-option
                      v-for="item in bankOption"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-form-item label="参与企业">
                <el-tree
                  :data="supplyList"
                  show-checkbox
                  default-expand-all
                  node-key="eid"
                  :props="defaultProps"
                  ref="tree"
                  @check-change="getCheckedKeys"
                  :check-strictly="check"
                >
                </el-tree>
              </el-form-item>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="createNetworkDialog = false">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </div>
        </el-dialog>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column prop="nname" label="流转网络名称" width="160">
      </el-table-column>
      <el-table-column prop="network_no" label="流转网络编号" width="160">
      </el-table-column>
      <el-table-column sortable prop="num" label="企业数量" width="100">
      </el-table-column>
      <el-table-column sortable prop="type" label="业务产品" width="160">
      </el-table-column>
      <el-table-column sortable prop="ename" label="融资机构" width="160">
      </el-table-column>
      <el-table-column sortable prop="status" label="状态" width="160">
        <template slot-scope="scope">
          <div>
            <switchOne
              :mystatus="scope.row.status"
              @changeSwitch="changeSwitch(scope.row)"
            ></switchOne>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="edit(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="编辑链单网络" :visible.sync="editFormVisible">
      <el-form :model="editForm" label-position="left" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="链单网络名称">
              <el-input
                v-model="editForm.name"
                autocomplete="off"
                style="width: 220px"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="网络编号">
              <el-input
                v-model="editForm.network_no"
                :disabled="true"
                style="width: 220px"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务产品类型">
              <el-select
                v-model="editForm.type"
                placeholder="请选择业务产品类型"
              >
                <el-option
                  label="授信保理链单"
                  value="授信保理链单"
                ></el-option>
                <el-option label="非授信链单" value="非授信链单"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="融资机构">
              <el-select
                v-model="editForm.bank_id"
                placeholder="请选择融资机构"
              >
                <el-option
                  v-for="item in bankOption"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="参与企业">
            <el-tree
              :data="supplyList"
              show-checkbox
              default-expand-all
              node-key="eid"
              :props="defaultProps"
              ref="tree"
              :check-strictly="check"
              @check-change="getCheckedKeys2"
              :default-checked-keys="editForm.eids"
            >
            </el-tree>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateForm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
    <!-- 编辑界面 -->
  </div>
</template>

<script>
import switchOne from '../../components/switchOne.vue'
import { deptList, deptSave, deptDelete } from '../../api/userMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      check: true,
      editFormVisible: false,
      defaultProps: {
        children: 'enterprises',
        label: 'ename'
      },
      supplyList: [],
      bankOption: [],
      createNetworkDialog: false,
      addForm: {
        name: "",
        type: "",
        bank_id: null,
        eids: [],
        coreId: "",
      },
      statusOptions: [
        {
          value: "0",
          label: "已禁用"
        },
        {
          value: "1",
          label: "已启用"
        },
      ],
      typeOptions: [
      ],
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        name: "",
        type: "",
        bank_id: null,
        eids: [],
        network_no: "",
        id: ""
      },
      formInline: {
        currentPage: 1,
        pageSize: 10,
        name: "",
        bank_id: null,
        status: "",
        coreId: ""
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination,
    switchOne
  },

  /**
   * 创建完毕
   */
  created() {
    this.addForm.coreId = JSON.parse(localStorage.getItem("userdata")).enterprise_id;
    this.formInline.coreId = JSON.parse(localStorage.getItem("userdata")).enterprise_id;
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    edit(row) {
      console.log(row)
      this.editFormVisible = true
      this.editForm.name = row.nname
      this.editForm.type = row.type
      this.editForm.bank_id = row.bank_id
      this.editForm.eids = row.eids
      this.editForm.network_no = row.network_no
      this.editForm.id = row.id
    },
    getCheckedKeys() {
      console.log(this.$refs.tree.getCheckedKeys());
      this.addForm.eids = this.$refs.tree.getCheckedKeys()
    },
    getCheckedKeys2() {
      console.log(this.$refs.tree.getCheckedKeys());
      this.editForm.eids = this.$refs.tree.getCheckedKeys()
    },
    getdata() {
      this.loading = true
      //查询所有链单网络
      this.$axios.post('/api/network/all/', this.formInline)
        .then(res => {
          console.log(res);
          this.loading = false
          if (res.data.code == 200) {
            this.$message({
              type: 'info',
              message: res.data.message
            })
            this.listData = res.data.data.list
            // 分页赋值
            this.pageparm.currentPage = this.formInline.currentPage
            this.pageparm.pageSize = this.formInline.pageSize
            this.pageparm.total = res.data.data.total
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('流转网络加载失败，请稍后再试！')
        }),
        //查询所有资金方
        this.$axios.post('/api/bank/allBank/')
          .then(res => {
            console.log(res);
            this.loading = false
            if (res.data.code == 200) {
              this.$message({
                type: 'info',
                message: res.data.message
              })
              this.bankOption = res.data.data
            }
          })
          .catch(err => {
            this.loading = false
            this.$message.error('资金方加载失败，请稍后再试！')
          }),
        //查询核心企业对应的供应链
        this.$axios.get('/api/supply/findById/' + this.addForm.coreId)
          .then(res => {
            console.log(res.data.data[0].enterprises);
            this.loading = false
            if (res.data.code == 200) {
              this.$message({
                type: 'info',
                message: res.data.message
              })
              this.supplyList = res.data.data[0].enterprises
            }
          })
          .catch(err => {
            this.loading = false
            this.$message.error('供应链加载失败，请稍后再试！')
          })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.currentPage = parm.currentPage
      this.formInline.pageSize = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.loading = true
      this.$axios.post('/api/network/findBy/', this.formInline)
        .then(res => {
          console.log(res);
          this.loading = false
          if (res.data.code == 200) {
            this.$message({
              type: 'info',
              message: res.data.message
            })
            this.listData = res.data.data.list
            // 分页赋值
            this.pageparm.currentPage = this.formInline.currentPage
            this.pageparm.pageSize = this.formInline.pageSize
            this.pageparm.total = res.data.data.total

          } else {
            this.$message({
              type: 'info',
              message: res.data.message
            })
          }
          this.formInline.name = ""
          this.formInline.bank_id = null
          this.formInline.status = ""

        })
        .catch(err => {
          this.loading = false
          this.$message.error('链单网络查询失败，请稍后再试！')
        })
    },
    // 添加链单网络
    submitForm() {
      console.log(this.addForm)
      this.$axios.post('/api/network/add/', this.addForm)
        .then(res => {
          if (res.data.code == 200) {
            this.getdata()
            this.createNetworkDialog = false
            this.addForm.name = ""
            this.addForm.type = ""
            this.addForm.bank_id = null
            this.addForm.eids = []
            this.$message({
              type: 'info',
              message: "流转网络添加成功"
            })
          }
        })
        .catch(err => {
          this.createNetworkDialog = false
          this.$message.error('流转网络保存失败，请稍后再试！')
        })
    },
    // 修改链单网络
    updateForm() {
      console.log(this.editForm)
      this.$axios.post('/api/network/update/', this.editForm)
        .then(res => {
          if (res.data.code == 200) {
            this.getdata()
            this.editFormVisible = false
            this.$message({
              type: 'info',
              message: "流转网络修改成功"
            })
          }
        })
        .catch(err => {
          this.editFormVisible = false
          this.$message.error('流转网络修改失败，请稍后再试！')
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    },
    //switch开关触发事件
    changeSwitch(row) {
      console.log(row.id)
      if (row.status == 1) {
        this.$confirm('确定要禁用吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$axios.post('/api/network/updateStatus/', row)
              .then(res => {
                if (res.data.code == 200) {
                  this.$message({
                    type: 'success',
                    message: '流转网络已禁用!'
                  })
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.loading = false
                this.$message.error('流转网络禁用失败，请稍后再试！')
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消禁用'
            })
          })
      } else {
        this.$confirm('确定要启用吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$axios.post('/api/network/updateStatus/', row)
              .then(res => {
                if (res.data.code == 200) {
                  this.$message({
                    type: 'success',
                    message: '流转网络已启用!'
                  })
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.loading = false
                this.$message.error('流转网络启用失败，请稍后再试！')
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消启用'
            })
          })
      }

    }
  }
}
</script>

<style scoped>
.el-row {
  margin-bottom: 20px;
  /* &:last-child {
    margin-bottom: 0;
  } */
}
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.create-course-dialog .el-dialog {
  margin-top: 40px !important;
  width: 1000px;
  height: 820px;
  position: relative;
}
.create-course-dialog .el-upload {
  width: 100px;
  position: relative;
  left: 5px;
  top: 5px;
}
.create-course-dialog img {
  width: 100%;
  height: 100%;
}
.create-course-dialog .el-dialog__title {
  font-size: 30px;
}
.create-course-dialog .el-form-item__label {
  font-size: 18px;
  width: 90px;
}
.create-course-dialog .el-form-item {
  margin-top: 10px;
}
.create-course-dialog .el-input {
  width: 250px;
}
.create-course-dialog .dialog-left {
  float: left;
  border: 1px #bababa solid;
  border-radius: 5px;
  width: 420px;
  height: 380px;
  position: relative;
}
.create-course-dialog .dialog-right {
  float: right;
  border: 1px #bababa solid;
  border-radius: 5px;
  width: 500px;
  height: 380px;
}
.create-course-dialog .dialog-footer {
  position: absolute;
  bottom: 20px;
  right: 30px;
}
.create-course-dialog .el-input-number .el-input {
  width: 100%;
}
.create-course-dialog .el-input-number {
  width: 100px;
}
.create-course-dialog .el-form-item__error {
  margin-left: 90px;
}
.el-popover {
  max-width: 860px;
  max-height: 760px;
  overflow: auto;
}
.small-img img {
  height: 100px;
}
.small-img {
  padding: 5px;
  float: left;
}
</style>


